<?php namespace Admin\Controller;
/**
 * 货品列表控制器
 */
 class GoodslistController extends CommonController
 {
 	protected $db;
 	/**
	 * 构造方法
	 */
	public function __init()
	{
		$this->db = new \Common\Model\Goodslist;
	}
 	/**
	 * 首页
	 */
	public function index()
	{
		//获得商品id
		$gid = Q('get.goods_id',0,'intval');
		//获取旧数据
		$goodslistData = Db::table('goods_list')->where('goodslist_goods_id',$gid)->get();
		foreach($goodslistData as $k=>$v)
		{
			$goodslistData[$k]['combine']=explode(',', $v['combine']);
		}
		foreach($goodslistData as $k=>$v)
		{
			foreach($v['combine'] as $kk=>$vv)
			{
				$goodslistData[$k]['combine'][$kk] = Db::table('goods_attr')->where('goodsattr_id',$vv)->pluck('goods_value');
			}
		}
		//获得属性ID
		
		$data = Db::table('goods_attr')
		        ->join('typeattr','goodsattr_typeattr_id','=','typeattr_id')
				->where('typeattr_type',1)
				->where('goodsattr_goods_id',$gid)
				->get();
		//获取规格
		$spec =  Db::table('typeattr')->where('typeattr_type_id',$data[0]['typeattr_type_id'])->where('typeattr_type',1)->get();		
		foreach($spec as $k=>$v)
		{
			$spec[$k]['typeattr_value'] = "";
		}	
		foreach($spec as $k=>$v)
		{
		  foreach($data as $kk=>$vv)
		  {
		  	if($vv['typeattr_id']==$v['typeattr_id'])
			{
				$spec[$k]['typeattr_value'][$vv['goodsattr_id']] = $vv['goods_value'];
			}
		  }
		}
		View::with('spec',$spec);
		View::with('goodslistData',$goodslistData);
		View::make();
	}
	/**
	 * 添加
	 */
	public function store()
	{
		$gid = Q('get.goods_id',0,'intval');	
		if(IS_POST)
		{
			if(!$this->db->store())
			{
				$this->error($this->db->getError());
			}else{
				$this->success('添加成功',U('index',array('goods_id'=>$gid)));
			}
		}	
	}
	/**
	 * 编辑
	 */
	public function edit()
	{
	    if(IS_POST)
		{
			if(!$this->db->edit())
			{
				$this->error($this->db->getError());
			}else{
				$this->error('修改成功',U('index',array('goods_id'=>$_GET['goods_id'])));
			}
		}
		$goodslist_id = Q('get.goodslist_id',0,'intval');
		$spec = Db::table('goods_list')->where('goodslist_id',$goodslist_id)->first();
		$spec['combine'] = explode(',', $spec['combine']);
		foreach($spec['combine'] as $k=>$v)
		{
			$spec['combine'][$k] = Db::table('goods_attr')
			                       ->join('typeattr','goodsattr_typeattr_id','=','typeattr_id')
			                       ->where('goodsattr_id',$v)->first();
		}
		View::with('spec',$spec);
		View::make();
	}
	/**
	 * 删除
	 */
	public function del()
	{
		$gid = Q('get.goodslist_id',0,'intval');
		$this->db->where('goodslist_id',$gid)->delete();
		$this->success('删除成功',U('index',array('goods_id'=>$_GET['goods_id'])));
	}
	/**
	 * 异步提交
	 */
	public function check()
	{
		if(IS_AJAX)
		{
			$data = 0;
			$remote = $_GET['remote'];
			$remote['spec'] = implode(',', $remote['spec']);
			$goodslistData = Db::table('goods_list')->where('goodslist_goods_id',$remote['gid'])->get();
			foreach($goodslistData as $k=>$v)
			{
				if($v['combine'] == $remote['spec'])
				{
					echo json_encode($data);	
				}
			}
		}
	}
 }
